package cn.net.shuxing.yifu.common.util;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {

    public static void deal(InputStream inputStream, ExcelDataDeal excelDataDeal)
    {
        try {
            // 解析每行结果在listener中处理
            ExcelListener listener = new ExcelListener(excelDataDeal);

            ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
            excelReader.read();
        } catch (Exception e) {

        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void deal(File excelFile, ExcelDataDeal excelDataDeal) throws FileNotFoundException {
        if (excelFile.exists())
        {
            InputStream inputStream = new FileInputStream(excelFile);
            try {
                // 解析每行结果在listener中处理
                ExcelListener listener = new ExcelListener(excelDataDeal);

                ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
                excelReader.read();
            } catch (Exception e) {

            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        else
        {
            throw new FileNotFoundException(excelFile.getAbsolutePath() + "没找到");
        }
    }

    public static class ExcelListener extends AnalysisEventListener{

        private List<Object> datas = new ArrayList<Object>();
        private ExcelDataDeal excelDataDeal;

        public ExcelListener(ExcelDataDeal excelDataDeal)
        {
            this.excelDataDeal = excelDataDeal;
        }

        public void invoke(Object object, AnalysisContext context) {
            System.out.println("当前行："+context.getCurrentRowNum());
            System.out.println(object);
            datas.add(object);//数据存储到list，供批量处理，或后续自己业务逻辑处理。
            doSomething(object);//根据自己业务做处理
        }
        private void doSomething(Object object) {
            excelDataDeal.doSomething((List<String>) object);
        }
        public void doAfterAllAnalysed(AnalysisContext context) {
            datas.clear();//解析结束销毁不用的资源
        }
        public List<Object> getDatas() {
            return datas;
        }
        public void setDatas(List<Object> datas) {
            this.datas = datas;
        }
    }

    @FunctionalInterface
    public static interface ExcelDataDeal{
        void doSomething(List<String> data);
    }

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("E:\\132.xls");
        deal(file,(x)->{
            x.forEach(System.out::println);
        });
    }
}
